package middleware

import (
	"encoding/json"
	"fmt"
	"goravel/app/models"

	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"github.com/goravel/framework/support/debug"
)

func OperateLog() http.Middleware {
	return func(ctx http.Context) {
		queryData := ctx.Request().Queries()

		queryJson, err := json.Marshal(queryData)
		if err != nil {
			debug.Dump(err.Error())
		}
		fmt.Println("querys: ", string(queryJson))
		// #####

		postData := ctx.Request().Origin().Body
		postJson, err := json.Marshal(postData)
		if err != nil {
			debug.Dump(err.Error())
		}
		fmt.Println("body: ", string(postJson))

		// #### auth获取用户
		var user models.User
		err2 := facades.Auth().User(ctx, &user)
		if err2 != nil {
			debug.Dump(err2.Error())
		}
		userJson, err3 := json.Marshal(user)
		if err3 != nil {
			debug.Dump(err3.Error())
		}

		fmt.Println("user: ", string(userJson))

		ctx.Request().Next()
	}
}
